﻿module Runner {
    export function buildHitPointBar<T extends OffenseDefense>(body: T) {
        //body.addChild(body.healthBar);
        body.healthBar = body.game.add.graphics(0, 0);

        body.addChild(body.healthBar);

        updateHitPointBar(body);
    }

    export function updateHitPointBar<T extends OffenseDefense>(body: T) {
        if (body._lastHitPoints !== body.hitPoints) {
            body.healthBar.clear();
            body.hitPoints = (body.hitPoints <= 0) ? 0 : body.hitPoints;
            var x = (body.hitPoints / body.maxHitPoints) * 100;

            var colour = Utils.RGBtoHex((x > 50 ? 1 - 2 * (x - 50) / 100.0 : 1.0) * 255, (x > 50 ? 1.0 : 2 * x / 100.0) * 255, 0);
            body.healthBar.beginFill(colour);
            body.healthBar.lineStyle(5, colour, 1);
            body.healthBar.moveTo(0, -5);
            body.healthBar.lineTo(32 /*tilesize*/ * body.hitPoints / body.maxHitPoints, -5);
            body.healthBar.lineStyle(1, colour, 1);
            body.healthBar.scale.x = body.healthBar.scale.y = 2;
        }

        body._lastHitPoints = body.hitPoints;
    }
}